(function() {
    angular
        .module('ydApp.user.center', [])
        .config(function($stateProvider) {
            //个人信息
            $stateProvider.state('userInfo', {
                url: '/userInfo',
                cache: false,
                controller: 'userInfoCtrl',
                templateUrl: 'views/userCenter/user-info.html'
            });

            //医生信息
            $stateProvider.state('userInfo-doc', {
                url: '/doctorInfo/:edit',
                cache: false,
                controller: 'userInfoCtrl',
                templateUrl: 'views/userCenter/doctor-info.html'
            });


            //我的账户
            $stateProvider.state('userAccountset', {
                url: '/userAccountSet',
                cache: false,
                controller: 'accountSetCtrl',
                templateUrl: 'views/userCenter/account-set-user.html'
            });

            //我的账户医生版
            $stateProvider.state('docAccountset', {
                url: '/docAccountSet',
                cache: false,
                controller: 'docAccountSetCtrl',
                templateUrl: 'views/userCenter/account-set-doc.html'
            });

            //我的消息
            $stateProvider.state('message', {
                url: '/message',
                cache: false,
                controller: 'messageCtrl',
                templateUrl: 'views/userCenter/message.html'
            });

            //消息详情
            $stateProvider.state('messageDetail', {
                url: '/messageDetail/:id?content?sendTimeString?sendUserId',
                cache: false,
                controller: 'messageDetailCtrl',
                templateUrl: 'views/userCenter/message-detail.html'
            });

            //意见反馈
            $stateProvider.state('advice', {
                url: '/advice',
                cache: false,
                controller: 'adviceCtrl',
                templateUrl: 'views/userCenter/advice.html'
            });

            // 意见反馈成功
            $stateProvider.state('adviceOk', {
                url: '/adviceOk',
                cache: false,
                controller: 'adviceCtrl',
                templateUrl: 'views/userCenter/advice-ok.html'
            });

            // 重置密码
            $stateProvider.state('resetPwd', {
                url: '/resetPwd',
                cache: false,
                controller: 'resetPwdCtrl',
                templateUrl: 'views/userCenter/reset-pwd.html'
            });

            // 关于
            $stateProvider.state('about', {
                url: '/about',
                cache: false,
                controller: 'aboutCtrl',
                templateUrl: 'views/userCenter/about.html'
            });

            //免责申明
            $stateProvider.state('declare', {
                url: '/declare.html',
                cache: true,
                controller: 'aboutCtrl',
                templateUrl: 'views/userCenter/declare.html'
            })

            // 版本介绍
            $stateProvider.state('version', {
                url: '/version',
                cache: false,
                controller: 'versionCtrl',
                templateUrl: 'views/userCenter/version.html'
            });

            //常见问题
            $stateProvider.state('qustion', {
                url: '/qustion',
                cache: false,
                controller: 'qustionCtrl',
                templateUrl: 'views/userCenter/qustion.html'
            });

            //问题详情
            $stateProvider.state('qustionDetail', {
                url: '/qustionDetail/:id',
                cache: false,
                controller: 'qustionDetailCtrl',
                templateUrl: 'views/userCenter/qustion-detail.html'
            });

            //开通会员
            $stateProvider.state('vipIndex', {
                url: '/vipIndex',
                cache: false,
                controller: 'vipCtrl',
                templateUrl: 'views/userCenter/vip-index.html'
            });

            // 开通会员支付
            $stateProvider.state('vipPay', {
                url: '/vipPay',
                cache: false,
                controller: 'vipCtrl',
                templateUrl: 'views/userCenter/vip-pay.html'
            });

            // 开通会员支付成功
            $stateProvider.state('vipOk', {
                url: '/vipOk',
                cache: false,
                controller: 'versionCtrl',
                templateUrl: 'views/userCenter/vip-ok.html'
            });

            //我的账户
            $stateProvider.state('account', {
                url: '/account',
                cache: false,
                controller: 'accountCtrl',
                templateUrl: 'views/userCenter/account.html'
            });

            // 账户记录
            $stateProvider.state('accountHistory', {
                url: '/accountHistory',
                cache: false,
                controller: 'accountHistoryCtrl',
                templateUrl: 'views/userCenter/account-history.html'
            });

            // 充值
            $stateProvider.state('recharge', {
                url: '/recharge',
                cache: false,
                controller: 'rechargeCtrl',
                templateUrl: 'views/userCenter/recharge.html'
            });

            // 充值成功
            $stateProvider.state('rechargeOk', {
                url: '/rechargeOk',
                cache: false,
                controller: 'versionCtrl',
                templateUrl: 'views/userCenter/recharge-ok.html'
            });

            //  充值失败
            $stateProvider.state('rechargeFail', {
                url: '/rechargeFail',
                cache: false,
                controller: 'versionCtrl',
                templateUrl: 'views/userCenter/recharge-fail.html'
            });

            // 充值
            $stateProvider.state('message-info', {
                url: '/message-info',
                cache: false,
                controller: 'messageInfoCtrl',
                templateUrl: 'views/userCenter/message-info.html'
            });

            // 提现
            $stateProvider.state('fetch', {
                url: '/fetch/:bankType?code',
                cache: false,
                controller: 'fetchCtrl',
                templateUrl: 'views/userCenter/fetch.html'
            });
            //银行卡列表
            $stateProvider.state('card', {
                url: '/card',
                cache: false,
                controller: 'cardCtrl',
                templateUrl: 'views/userCenter/card.html'
            });
            //添加银行卡
            $stateProvider.state('card-add', {
                url: '/card-add',
                cache: false,
                controller: 'cardAddCtrl',
                templateUrl: 'views/userCenter/card-add.html'
            });

        })
        // .filter("actionMode", function(input) {
        //     return function(input) {
        //         var rValue;
        //         switch (input) {
        //             case 0:
        //                 rValue = "充值";
        //                 break;
        //             case 1:
        //                 rValue = "消费";
        //                 break;
        //             case 2:
        //                 rValue = "提现";
        //                 break;
        //             default:
        //                 rValue = "";

    //         }
    //         return rValue;
    //     }
    // })
    // .controller("userCenterCtrl", userCenterCtrl)
    .controller("userInfoCtrl", userInfoCtrl)
        // .controller("docCenterCtrl", docCenterCtrl)
        .controller("messageCtrl", messageCtrl)
        .controller("aboutCtrl", aboutCtrl)
        .controller("resetPwdCtrl", resetPwdCtrl)
        .controller("versionCtrl", resetPwdCtrl)
        .controller("docAccountSetCtrl", docAccountSetCtrl)
        .controller("messageDetailCtrl", messageDetailCtrl)
        .controller("resetPwdCtrl", resetPwdCtrl)
        .controller("qustionCtrl", qustionCtrl)
        .controller("qustionDetailCtrl", qustionDetailCtrl)
        .controller("messageInfoCtrl", messageInfoCtrl)
        .controller("vipCtrl", vipCtrl)
        .controller("accountCtrl", accountCtrl)
        //账户设置
        .controller("accountSetCtrl", accountSetCtrl)
        .controller("accountHistoryCtrl", accountHistoryCtrl)
        .controller("adviceCtrl", adviceCtrl)
        .controller("rechargeCtrl", rechargeCtrl)
        .controller("fetchCtrl", fetchCtrl)
        .controller('cardCtrl', cardCtrl)
        .controller('cardAddCtrl', cardAddCtrl);



    // function userCenterCtrl($scope, $state) {
    //     var vm = $scope.vm = {};

    //     $scope.go = function(state) {
    //         $state.go(state);
    //     }
    // }


    function userInfoCtrl($scope, $stateParams, localStore, xhr, $state, popup, Cameranav, fileTransfer, $ionicLoading, xhr, $cordovaToast) {

        var vm = $scope.vm = {};
        if ($stateParams.edit) {
            vm.edit = true;
        } else {
            vm.edit = false;
        }
        vm.user = localStore.get("user");
        $scope.logout = function() {
            localStore.del("user");
            console.log(localStore.get("user"));
            $scope.updateOnLine(0);
            $state.go("login");
        }

        $scope.save = function() {
            console.log("save user info")
            if (localStore.get("userType") == 2) {
                popup.confirm("您正在修改个人信息，回复咨询、鉴定及金额提现等服务将暂停使用，是否继续修改？")
                    .then(function(res) {
                        if (res) {
                            $scope.update();
                        }
                    });
            } else {
                $scope.update();

            }

        }

        $scope.update = function() {
            console.log("update vm.user")
            console.log(JSON.stringify(vm.user));
            var url = vm.user.userType == 1 ? "HealthInquiry/com/user/updateUserInfo" : "HealthInquiry/com/doctor/update";
            xhr.get(url, vm.user)
                .then(function() {
                    vm.edit = false;
                    $cordovaToast.showShortCenter("修改成功");
                    localStore.save("user", vm.user);
                });
        }

        $scope.go = function(router, state) {
            $state.go(router, { state: state });
        }


        $scope.goDocInfo = function(router, state) {
            console.log("router:" + router)
            $state.go(router, { state: state, edit: 1 })
        }

        $scope.updateOnLine = function(isOnline) {
            console.log("isOnline:" + isOnline)
            vm.user = localStore.get("user");
            console.log(vm.user)
            vm.user.isOnline = isOnline;
            xhr.get("HealthInquiry/com/user/updateIsonline", {
                id: vm.user.id,
                is_online: vm.user.isOnline
            }).then(function(result) {
                // $cordovaToast.showShortCenter("更新状态成功");
                if (isOnline == 0) {
                    localStore.del("user");
                    localStore.del("userType");
                } else {
                    localStore.save('user', vm.user);
                }
            });
        }



        $scope.getAvatar = function() {
            vm.user = localStore.get("user");
            Cameranav.getPicture(function(imgUri) {
                $ionicLoading.show({
                    template: '正在上传…'
                });
                fileTransfer.upload(imgUri, vm.user.id, function(data) {
                    if (data.successful) {
                        vm.user.headImgUrl = data.data.picUrl;
                        localStore.save("user", vm.user);
                        $scope.update();
                        // xhr.get(url, vm.user).then(function(result) {
                        //     localStore.save("user", vm.user);
                        //     $ionicLoading.hide();
                        // }).then(function(result) {
                        //     $ionicLoading.show({
                        //         template: '上传失败'
                        //     });
                        //     $ionicLoading.hide();
                        // });
                        $ionicLoading.hide();

                    } else {
                        $ionicLoading.hide();

                    }
                })
            })
        }


    }

    // function docCenterCtrl($scope, $state, localStore) {
    //     var vm = $scope.vm = {};

    //     vm.go = function(state) {
    //         $state.go(state);
    //     }
    // }


    function docAccountSetCtrl($scope, $state, localStore, xhr, $cordovaToast) {
        var vm = $scope.vm = {};
        vm.user = localStore.get('user');
        vm.telphone = vm.user.code;

        $scope.logout = function() {
            console.log(localStore.get("user"));
            $scope.updateOnLine(0);
            localStore.del("user");
            localStore.del('userType');
            $state.go("login");
        }


        // $scope.getDoc = function() {
        //     console.log(vm.user.id);
        //     // if (vm.user.isOnline == 0) {
        //     //         vm.user.isOnline = 1;
        //     //     }
        //     xhr.get('HealthInquiry/com/doctor/findById', { id: vm.user.id }).then(function(result) {
        //         vm.user = result.data;
        //         vm.user.userType = 2;
        //         vm.user.code = vm.telphone;
        //         if (vm.user.isOnline == 0) {
        //             vm.user.isOnline = 1;
        //         }
        //         console.log(vm.user);
        //         localStore.save("user", vm.user);
        //     });
        // }


        $scope.updateOnLine = function(isOnline) {
            vm.user = localStore.get("user");
            if (!vm.user) {
                return;
            }

            vm.user.isOnline = isOnline;
            xhr.get("HealthInquiry/com/user/updateIsonline", {
                id: vm.user.id,
                is_online: vm.user.isOnline
            }).then(function(result) {
                // $cordovaToast.showShortCenter("更新状态成功");
                if (isOnline == 0) {
                    localStore.del("user");
                } else {
                    localStore.save('user', vm.user);
                }
            });
        }

        $scope.updateCallCount = function() {
            // vm.user.callCount=callCount;
            xhr.get("HealthInquiry/com/doctor/updateCallCount", {
                id: vm.user.id,
                callCount: vm.user.callCount
            }).then(function(result) {
                localStore.save('user', vm.user);
                $cordovaToast.showShortCenter("更新成功");

            });
        }

        // $scope.getDoc();

    }

    function accountSetCtrl($scope, $state, localStore, xhr, $cordovaToast) {
        var vm = $scope.vm = {};
        vm.user = localStore.get('user');
        $scope.go = function(router) {
            $state.go(router);
        }

        // $scope.getUser = function() {
        //     xhr.get('HealthInquiry/com/user/findById', { id: vm.user.id }).then(function(result) {
        //         vm.user = result.data;
        //         vm.user.userType = 1;
        //         localStore.save("userType",vm.user.userType);
        //         vm.user.isOnline = vm.user.is_online;
        //         if (vm.user.isOnline == 0) {
        //             vm.user.isOnline = 1;
        //         }
        //         console.log(vm.user);
        //         localStore.save("user", vm.user);
        //     });
        // }

        $scope.downloadImg = function(img) {
            return config.api + "HealthInquiry" + img;
        }

        $scope.updateOnLine = function(status) {
            vm.user.isOnline = status;
            xhr.get('HealthInquiry/com/user/updateIsonline', {
                id: vm.user.id,
                is_online: status
            }).then(function(result) {
                // $cordovaToast.showShortCenter("更新状态成功");
                //离线
                console.log("status:" + status);
                if (status == 0) {
                    localStore.del("user");
                } else {
                    localStore.save('user', vm.user);
                }
            });;
        }



        $scope.logout = function() {
                localStore.del("user");
                localStore.del('localStore');
                $scope.updateOnLine(0)
                $state.go("login");
            }
            // $scope.getUser();
    }

    function messageCtrl($scope, $state, localStore, xhr, $cordovaToast) {
        var vm = $scope.vm = {
            nodata: false,
            list: []
        };
        vm.user = localStore.get("user");

        vm.condition = {
            pageIndex: 0,
            pageSize: 10,
            userId: vm.user.id
                // sortField:'',
                // sortOrder:'asc',
        }
        vm.showMsg = false;

        $scope.doRefresh = function() {
            vm.condition.pageIndex = 0;
            vm.list = [];
            $scope.query().then(function() {
                $scope.$broadcast('scroll.refreshComplete');
            });

        }

        $scope.loadMore = function() {
            console.log(vm.condition.pageIndex);
            vm.condition.pageIndex++;
            $scope.query().then(function() {
                $scope.$broadcast('scroll.infiniteScrollComplete');
            });

        }


        $scope.query = function() {
            return xhr.get('HealthInquiry/com/sysMessage/findByCondition', vm.condition).then(function(result) {
                if (result.data.length <= 0) {
                    vm.nodata = true;
                } else {
                    vm.list = vm.list.concat(result.data);
                    vm.nodata = false;
                }
            });
        }

        $scope.showMessage = function(message) {
            xhr.get("HealthInquiry/com/sysMessage/readById", {
                msgId: message.id,
                userId: vm.user.id
            }).then(function(result) {
                $state.go("messageDetail", {
                    id: message.id,
                    sendUserId: message.sendUserId,
                    content: message.content,
                    sendTimeString: message.sendTimeString
                })
            });

        }


        $scope.readAll = function() {
            xhr.get("HealthInquiry/com/sysMessage/readAll", {
                userId: vm.user.id
            }).then(function() {
                vm.showMsg = false;
                $cordovaToast.showShortCenter("操作成功");
                $scope.query();
            });
        }


        $scope.deleteAll = function() {
            xhr.get("HealthInquiry/com/sysMessage/deleteAll", {
                userId: vm.user.id
            }).then(function() {
                vm.showMsg = false;
                $cordovaToast.showShortCenter("操作成功");
                $scope.query();
            });
        }

        $scope.query();


    }

    function messageDetailCtrl($scope, xhr, $stateParams) {
        var vm = $scope.vm = {};
        vm.message = {
            id: $stateParams.id,
            content: $stateParams.content,
            sendUserId: $stateParams.sendUserId,
            sendTimeString: $stateParams.sendTimeString
        }
    }


    function resetPwdCtrl($scope, xhr, $stateParams, popup, $timeout, $state, $ionicHistory, $cordovaToast, localStore) {
        var vm = $scope.vm = {};
        vm.enable = true;


        $scope.getCode = function() {
            if (!vm.enable) {
                return;
            }
            if (vm.code) {
                xhr.get('HealthInquiry/com/sms/send', { phone: vm.code }).then(function(result) {
                    vm.enable = false;
                    vm.time = 60,
                        $scope.timeInteval();
                });
            } else {
                popup.alert("请输入手机号码");
            }

        }

        $scope.timeInteval = function() {
            vm.time--;
            if (vm.time > 0) {
                $timeout($scope.timeInteval, 1000);
            } else {
                vm.enable = true;
            }
        }


        $scope.resetPwd = function() {
            if (!vm.code) {
                popup.alert("请输入手机号码");
                return;
            }

            if (!vm.valiCode) {
                popup.alert("请输入验证码");
                return;
            }

            if (!vm.password) {
                popup.alert("请输入验密码");
                return;
            }
            if (!angular.equals(vm.password, vm.repassword)) {
                popup.alert("两次密码输入不一致");
                return;
            }

            xhr.get("HealthInquiry/com/user/updatepwd", {
                code: vm.code,
                valiCode: vm.valiCode,
                password: vm.password
            }).then(function(result) {
                // $cordovaToast.showShortCenter(result.resultMsg);
                // popup.alert(result.resultMsg);
                // $ionicHistory.goBack();
                localStore.save("message", {
                    title: '修改密码',
                    content: '您已修改成功',
                    back: 'tabs.user'
                })
                $state.go("message-info");
            });
        }
    }

    function adviceCtrl($scope, $state, xhr, popup, $ionicHistory, localStore) {
        var vm = $scope.vm = {};

        $scope.save = function() {
            xhr.get("HealthInquiry/com/feedback/saveOrUpdate", {
                email: vm.email,
                content: vm.content,
                phone: vm.phone
            }).then(function(result) {
                localStore.save("message", {
                    title: '意见反馈',
                    content: '您的建议我们已经收到,非常感谢!',
                    back: 'tabs.user'
                })
                $state.go("message-info");
            })
        }
    }


    function aboutCtrl($cordovaInAppBrowser, $scope) {
        var vm = $scope.vm = {
            'declareUrl': config.api + "HealthInquiry/view/health/disclaimer_show.jsp"
        }
        console.log($scope.vm.declareUrl)
        var options = {
            location: 'no',
            clearcache: 'no',
            toolbar: 'yes'
        };
        $scope.open = function() {
            $cordovaInAppBrowser.open(config.api + "HealthInquiry/view/health/disclaimer_show.jsp", '_blank', options);
        }
    }

    function qustionCtrl(xhr, $scope, $state) {
        var vm = $scope.vm = {};
        vm.page = 0;
        xhr.get("HealthInquiry/com/question/findByCondition", {
            page: vm.page
        }).then(function(result) {
            vm.list = result.data;
        });

        $scope.toDetail = function(q) {
            $state.go("qustionDetail", {
                id: q.id
            });
        }
    }

    function qustionDetailCtrl($scope, xhr, $stateParams) {
        var vm = $scope.vm = {};
        xhr.get("HealthInquiry/com/question/findById", {
            id: $stateParams.id
        }).then(function(result) {
            vm.qustion = result.data;
        });
    }


    function messageInfoCtrl($scope, localStore) {
        var vm = $scope.vm = {};
        vm.message = localStore.get("message");
        vm.message = angular.isUndefined(vm.message) ? {} : vm.message;
    }


    function vipCtrl($scope, $cordovaToast, pay, localStore, xhr, popup, $state) {
        var vm = $scope.vm = {};
        vm.user = localStore.get("user");
        vm.money = 100;
        $scope.payVip = function() {
            xhr.get("HealthInquiry/com/user/openVip", {
                userId: vm.user.id,
                money: 100
            }).then(function(res) {
                console.log(res);
                popup.alert(res.resultMsg);
                $state.go('tabs.user');
            }, function(err) {
                $state.go("recharge");
            });
        }
    }

    function accountCtrl($scope, $cordovaToast, xhr, localStore, $state) {

        var vm = $scope.vm = {};
        vm.userType = localStore.get("userType")
        vm.user = localStore.get("user");

        xhr.get("HealthInquiry/com/account/findBalanceByUserId", {
            userId: localStore.get("user").id
        }).then(function(result) {
            vm.balance = result.data.balance;
        });


    }


    function accountHistoryCtrl($scope, xhr, localStore) {
        var vm = $scope.vm = {};
        vm.user = localStore.get("user");
        $scope.query = function() {
            xhr.get("HealthInquiry/com/accountDetail/findByCondition", {
                userId: vm.user.id
            }).then(function(result) {
                vm.list = result.data;
                angular.forEach(vm.list, function(value, key) {
                    value.time = value.actionTimeString.substring(0, 10);
                });
                console.log(result);
            });
        }
        $scope.query();
    }

    function rechargeCtrl($scope, xhr, localStore, pay, $state) {
        var vm = $scope.vm = {};
        vm.money = 100;
        vm.user = localStore.get("user");
        $scope.pay = function() {
            var reg = /^\d+(\.\d+)?$/;
            if (!reg.test(vm.money) || vm.money <= 0) {
                $cordovaToast.showShortCenter("请输入正确的金额");
                return;
            }
            xhr.get("HealthInquiry/com/alipay/getOders", {
                userId: vm.user.id,
                money: vm.money
            }).then(function(result) {
                var orderNo = result.data.orderNo;
                pay.alipay("余额充值", "余额充值", vm.money, orderNo, function(result) {
                    $state.go('rechargeOk')
                }, function(error) {

                });
            });
        }
    }


    function fetchCtrl($scope, xhr, localStore, $state, popup, $stateParams) {
        var vm = $scope.vm = {};
        vm.model = {};
        vm.user = localStore.get("user");
        vm.card = {
                'code': $stateParams.code,
                'bankType': $stateParams.bankType
            }
            //查询余额
        xhr.get("HealthInquiry/com/account/findBalanceByUserId", {
            userId: vm.user.id
        }).then(function(result) {
            vm.balance = result.data.balance;
        });

        console.log(vm.card);

        //传了银行卡信息
        if (vm.card.code && vm.card.bankType) {

        }
        //没有传
        else {
            xhr.get('HealthInquiry/com/accountCard/findByUserId', {
                'userId': vm.user.id
            }).then(function(res) {
                if (res.data.length <= 0) {
                    popup.alert('您还没有绑定银行卡，请至少先绑定一张银行卡');
                    $state.go('card-add');
                } else {
                    vm.card = res.data[0];
                }
            });
        }

        //提现申请
        $scope.save = function() {
            var reg = /^[0-9]+$/;
            if (!reg.test(vm.model.money) || vm.model.money <= 0) {
                alert("请输入正确的金额");
                return;
            }
            vm.model.userId = vm.user.id;
            xhr.get("HealthInquiry/com/account/withdraw", vm.model).then(function(res) {
                if (res.successful) {
                    popup.alert(res.resultMsg);
                    $state.go("account");
                }
            });
        }

        $scope.toCard = function() {
            $state.go('card');
        }

    }

    function cardCtrl($scope, xhr, localStore, $state, $cordovaToast, popup) {
        var vm = $scope.vm = {};
        vm.user = localStore.get("user");
        $scope.init = function() {
            xhr.get('HealthInquiry/com/accountCard/findByUserId', {
                'userId': vm.user.id
            }).then(function(res) {
                vm.cardList = res.data;
            });
        }

        $scope.toFetch = function(card) {
            $state.go("fetch", {
                bankType: card.bankType,
                code: card.code
            });
        }
        $scope.init();

        $scope.delCard = function(card) {
            popup.confirm("是否删除该卡片").then(function(res) {
                if (res) {
                    xhr.get("HealthInquiry/com/accountCard/delete", {
                        'id': card.id
                    }).then(function(res) {
                        if (res.successful) {
                            $cordovaToast.showShortCenter("删除成功");
                            $scope.init();
                        }
                    });
                }
            });

        }
    }

    function cardAddCtrl($scope, xhr, localStore, $state, $timeout, popup) {
        var vm = $scope.vm = {};
        vm.user = localStore.get("user");
        vm.agree = false;
        vm.model = {
            userId: vm.user.id,
            phone: vm.user.code,
            // name: vm.user.name,
            bankType: "中国银行"
        };
        vm.enable = true;


        $scope.getCode = function() {
            if (!vm.enable) {
                return;
            }
            if (vm.model.phone) {
                xhr.get('HealthInquiry/com/sms/send', {
                    phone: vm.model.phone
                }).then(function(result) {
                    vm.time = 60;
                    $scope.timeInteval();
                    vm.enable = false;
                });
            } else {
                popup.alert("请输入手机号码");
            }

        }

        $scope.timeInteval = function() {
            vm.time--;
            if (vm.time > 0) {
                $timeout($scope.timeInteval, 1000);
            } else {
                vm.enable = true;
            }
        }


        $scope.saveCard = function() {

            // var reg = /^[0-9]+$/;
            if (!vm.model.name) {
                popup.alert("请输入姓名");
                return;
            }
            var reg = /^\d{14,22}$/;

            if (!reg.test(vm.model.code)) {
                popup.alert("请输入正确的银行卡号");
                return;
            }
            reg = /^1\d{10}$/;
            if (!reg.test(vm.model.phone)) {
                popup.alert("请输入正确的手机号");
                return;
            }
            if (!vm.model.valiCode) {
                popup.alert("请输入验证码");
                return;
            }
            if (!vm.agree) {
                popup.alert("请同意服务协议");
                return;
            }
            xhr.get("HealthInquiry/com/accountCard/saveOrUpdate", vm.model).then(function(res) {
                if (res.successful) {
                    popup.alert("保存成功");
                    $state.go("card");
                } else {

                }
            });

        }

    }

})();
